package com.yanqu.road.dao.impl.activity.drawPictrues;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.drawPictures.UserPicturesComb;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DrawPicturesUserPicturesCombDataDaoImpl extends TempDataObjectDao<UserPicturesComb> {
    @Override
    protected boolean addData(UserPicturesComb data) {
        
        String sql = "insert into t_u_activity_picturescombdata(activityId,userId,combId,times) values(?,?,?,?)";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCombId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTimes()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(UserPicturesComb data) {
        String sql = "update t_u_activity_picturescombdata set times=? where activityId=? and userId=? and combId=?";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCombId()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserPicturesComb parseData(ResultSet rs) throws SQLException {
        UserPicturesComb comb = new UserPicturesComb();
        comb.setActivityId(rs.getInt("activityId"));
        comb.setUserId(rs.getLong("userId"));
        comb.setCombId(rs.getInt("combId"));
        comb.setTimes(rs.getInt("times"));
        return comb;
    }

    public Map<Integer, Map<Integer, UserPicturesComb>> getDrawPicturesUserPicturesCombData(List<Integer> activityIdList, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, UserPicturesComb>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_activity_picturescombdata where userId = ? and activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserPicturesComb data = parse(rs);
                if (!dataMap.containsKey(data.getActivityId())) {
                    dataMap.put(data.getActivityId(), new ConcurrentHashMap<>());
                }
                dataMap.get(data.getActivityId()).put(data.getCombId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
